package com.zxbit.flash.presenter;

import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.mvpview.LoginMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ExceptionHandle;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.FaceBookLoginResponse;
import com.zxbit.flash.response.LoginResponse;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class LoginPresenter extends BasePresenter {
    private LoginMvpView mvpView;

    public LoginPresenter(LoginMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("userName", username);
        map.put("passWord", password);
        toSubScribe(req.passwordLogin(ParamsFactory.getCommonParams(map)), new CustomConsumer<LoginResponse>() {
            @Override
            public void call(LoginResponse loginResponse) {
                mvpView.onGetDataNext(loginResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                ExceptionHandle.handleException(throwable);
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void facebookLogin(String authCode) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("channel", BuildConfig.channel);
        paramsMap.put("authCode", authCode);
        toSubScribe(req.accountKitLogin(BuildConfig.channel, ParamsFactory.getCommonParams(paramsMap)), new CustomConsumer<FaceBookLoginResponse>() {
            @Override
            public void call(FaceBookLoginResponse faceBookLoginResponse) {
                mvpView.onGetDataNext(faceBookLoginResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

}
